import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:network_capture/network_capture.dart';
import 'package:qr_code_app/common/constant/color_config.dart';
import 'package:qr_code_app/common/route/main_router.dart';
import 'package:qr_code_app/common/service/app_data.dart';
import 'package:qr_code_app/common/service/app_service.dart';
import 'package:qr_code_app/common/service/qrcode_bloc_observer.dart';
import 'package:qr_code_app/module/diary/home_page.dart';
import 'package:qr_code_app/module/login/login_page.dart';
import 'package:todo_flutter/todo_app.dart';
import 'package:todo_flutter/todo_flutter.dart';

void main() {
  startApp();
}

Future<void> startApp() async {
  WidgetsFlutterBinding.ensureInitialized();
  await PreferencesUtil.init();

  ///透明状态栏、文字黑色
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.dark,
    ),
  );
  _switchEnv();

  GlobalKey<NavigatorState> navigatorKey = GlobalKey();
  runApp(
    RefreshConfiguration(
      headerBuilder: () => const MaterialClassicHeader(),
      hideFooterWhenNotFull: true,
      child: NetworkCaptureApp(
        navigatorKey: navigatorKey,
        enable: kDebugMode,
        child: TodoApp(
          home: LoginPage(),
          // MyHomePage(),
          designSize: const Size(390, 844),
          navigatorKey: navigatorKey,
          libData: TodoLibData(
            placeholderColor: ColorsConfig.ff161619.withOpacity(0.22),
            inputTextColor: ColorsConfig.ff161619,
            buttonHeight: 52,
          ),
          theme: ThemeData(
            ///app主题色
            useMaterial3: false,
            primaryColor: ColorsConfig.primaryColor,
            progressIndicatorTheme: const ProgressIndicatorThemeData(
              color: ColorsConfig.primaryColor,
            ),

            /// 光标颜色
            textSelectionTheme: const TextSelectionThemeData(
              cursorColor: ColorsConfig.primaryColor,
            ),
          ),
        ),
      ),
    ),
  );
  MainRouter.init();
  Bloc.observer = QrCodeBlocObserver();
  AppService.instance;
}

///切换环境
void _switchEnv() {
  final env = AppData.getEnv();
  if (ObjectUtil.isNotEmpty(env)) {
    Env.env = Env.fromEnv(env);
  }
}
